package boundary.tui;

import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

import boundary.IUI;
import entity.OperatorDTO;

public class TUI implements IUI {
	
	private Scanner consoleScanner;

	public TUI() {
		consoleScanner = new Scanner(System.in);
	}

	@Override
	public String[] login() {
		String[] loginOptions = new String[2];
		showMessage("LOGIN:");
		loginOptions[0] = Integer.toString(getInteger("User ID: "));
		loginOptions[1] = getString("Password: ");
		return loginOptions;
	}

	@Override
	public int userAction() {
		// operator
		return getInteger(
			"Type number action:\n" +
			"1: Change Password\n" +
			"2: Weight Application\n" +
			"0: Close\n");
	}
	
	@Override
	public int adminAction() {
		// admin
		return getInteger(
			"Type number action:\n" +
			"1: Show Operators\n" +
			"2: Show Operator\n" +
			"3: Add Operator\n" +
			"4: Update Operator\n" +
			"5: Delete Operator\n" +
			"0: Close\n");
	}
	
	@Override
	public void showOperators(List<OperatorDTO> operatorList) {
		for (OperatorDTO operator : operatorList) {
			System.out.println("User ID: " + operator.getOprID() + " - Name: "
					+ operator.getOprNavn() + " - Initials: "
					+ operator.getIni() + " - CPR: " + operator.getCpr()
					+ " - Password: " + operator.getPassword());
		}
	}

	@Override
	public void showOperator(OperatorDTO operator) {
		System.out.println("User ID: " + operator.getOprID() + " - Name: "
				+ operator.getOprNavn() + " - Initials: " + operator.getIni()
				+ " - CPR: " + operator.getCpr() + " - Password: "
				+ operator.getPassword());

	}

	@Override
	public int selectUser() {
		return getInteger("Enter the User ID of the user you want to select: ");
	}
	
	@Override
	public OperatorDTO createOperator() {
		String[] info = new String[2];
		info[0] = getString("Name: ");
		info[1] = getCpr();
		OperatorDTO operator = new OperatorDTO(0, info[0], info[1], logic.PasswordGenerator.generatePassword());
		return operator;
	}

	@Override
	public OperatorDTO updateOperator(OperatorDTO opr) {
		String[] info = new String[3];
		
		info[0] = getString("Name: ");
		info[1] = getCpr();

		int i = 0;
		do {
			if (i > 0) {
				showMessage("Your password must comply with our standards.");
			}
			info[2] = getString("Password: ");
			i = 1;
		} while (!logic.PasswordGenerator.checkPassword(info[2]));
		
		// TODO: Create DTO
		OperatorDTO operator = new OperatorDTO(opr.getOprID(), info[0], info[1], info[2]);
		return operator;
	}

	@Override
	public String[] changePassword() {
		String[] passwords = new String[3];

		passwords[0] = getString("Current password: ");

		int j = 0;
		do {
			if (j > 0)
				showMessage("The entries for your new password did not match. Please try again.");

			int i = 0;
			do {
				if (i > 0)
					showMessage("Your password must comply with our standards.");

				passwords[1] = getString("New password: ");
				i = 1;
			} while (!logic.PasswordGenerator.checkPassword(passwords[1]));

			passwords[2] = getString("New password again: ");
			j = 1;

		} while (!passwords[1].equals(passwords[2]));
		return passwords;
	}

	@Override
	public double[] weightApp() {
		double[] weight = new double[2];

		weight[1] = getDouble("Tare weight in kilograms: ");
		weight[0] = getDouble("Gross weight in kilograms: ");

		return weight;
	}



	@Override
	public void success(String message) {
		showMessage("SUCCESS: " + message);
	}

	@Override
	public void error(String message) {
		showMessage("ERROR: " + message);
	}

	@Override
	public void showNetWeight(Double netWeight) {
		showMessage("Net Weight: " + Double.toString(netWeight));
	}

	private void showMessage(String message) {
		System.out.println(message);
	}

	private String getString(String message) {
		System.out.print(message);
		return consoleScanner.next();
	}

	private int getInteger(String message) {
		System.out.print(message);
		int i = 0;
		do {
			if (i > 0)
				error("Have to be integer");
			try{
				
				return consoleScanner.nextInt();
				
			} catch (InputMismatchException e){
				i = 1;
			}
		} while (true);

	}

	private double getDouble(String msg) {
		System.out.print(msg);
		return consoleScanner.nextDouble();
	}

	private String getCpr(){
		String cpr = "";
		do {
			cpr = getString("CPR (without dash): ");
		} while (cpr.length() != 10);
		return cpr;
	}
	
}
